// 收藏功能工具函数

/**
 * 获取用户收藏列表
 * @param {string} userId 用户ID
 * @returns {Array} 收藏列表
 */
function getFavorites(userId) {
  if (!userId) return []
  return wx.getStorageSync(`favorites_${userId}`) || []
}

/**
 * 添加到收藏
 * @param {string} userId 用户ID
 * @param {Object} song 歌曲对象
 * @returns {boolean} 是否成功
 */
function addToFavorites(userId, song) {
  if (!userId || !song) return false
  
  const storageKey = `favorites_${userId}`
  let favorites = getFavorites(userId)
  
  // 检查是否已收藏
  const existingIndex = favorites.findIndex(fav => fav.id === song.id)
  if (existingIndex > -1) {
    return false // 已经收藏了
  }
  
  // 添加到收藏列表开头
  favorites.unshift({
    ...song,
    favoriteTime: new Date().getTime()
  })
  
  // 限制收藏数量（最多500首）
  if (favorites.length > 500) {
    favorites = favorites.slice(0, 500)
  }
  
  wx.setStorageSync(storageKey, favorites)
  return true
}

/**
 * 从收藏中移除
 * @param {string} userId 用户ID
 * @param {number} songId 歌曲ID
 * @returns {boolean} 是否成功
 */
function removeFromFavorites(userId, songId) {
  if (!userId || !songId) return false
  
  const storageKey = `favorites_${userId}`
  let favorites = getFavorites(userId)
  
  const existingIndex = favorites.findIndex(fav => fav.id === songId)
  if (existingIndex === -1) {
    return false // 没有收藏
  }
  
  favorites.splice(existingIndex, 1)
  wx.setStorageSync(storageKey, favorites)
  return true
}

/**
 * 切换收藏状态
 * @param {string} userId 用户ID
 * @param {Object} song 歌曲对象
 * @returns {Object} {success: boolean, isFavorited: boolean, message: string}
 */
function toggleFavorite(userId, song) {
  if (!userId) {
    return {
      success: false,
      isFavorited: false,
      message: '请先登录'
    }
  }
  
  if (!song) {
    return {
      success: false,
      isFavorited: false,
      message: '歌曲信息错误'
    }
  }
  
  const favorites = getFavorites(userId)
  const isCurrentlyFavorited = favorites.some(fav => fav.id === song.id)
  
  if (isCurrentlyFavorited) {
    const success = removeFromFavorites(userId, song.id)
    return {
      success,
      isFavorited: false,
      message: success ? '已取消收藏' : '取消收藏失败'
    }
  } else {
    const success = addToFavorites(userId, song)
    return {
      success,
      isFavorited: true,
      message: success ? '收藏成功' : '收藏失败'
    }
  }
}

/**
 * 检查歌曲是否已收藏
 * @param {string} userId 用户ID
 * @param {number} songId 歌曲ID
 * @returns {boolean} 是否已收藏
 */
function isFavorited(userId, songId) {
  if (!userId || !songId) return false
  
  const favorites = getFavorites(userId)
  return favorites.some(fav => fav.id === songId)
}

/**
 * 清空收藏列表
 * @param {string} userId 用户ID
 * @returns {boolean} 是否成功
 */
function clearFavorites(userId) {
  if (!userId) return false
  
  const storageKey = `favorites_${userId}`
  wx.removeStorageSync(storageKey)
  return true
}

/**
 * 格式化收藏时间
 * @param {number} timestamp 时间戳
 * @returns {string} 格式化后的时间
 */
function formatFavoriteTime(timestamp) {
  const now = new Date().getTime()
  const diff = now - timestamp
  
  const minute = 60 * 1000
  const hour = 60 * minute
  const day = 24 * hour
  const month = 30 * day
  
  if (diff < minute) {
    return '刚刚收藏'
  } else if (diff < hour) {
    return `${Math.floor(diff / minute)}分钟前收藏`
  } else if (diff < day) {
    return `${Math.floor(diff / hour)}小时前收藏`
  } else if (diff < month) {
    return `${Math.floor(diff / day)}天前收藏`
  } else {
    const date = new Date(timestamp)
    return `${date.getFullYear()}年${date.getMonth() + 1}月收藏`
  }
}

module.exports = {
  getFavorites,
  addToFavorites,
  removeFromFavorites,
  toggleFavorite,
  isFavorited,
  clearFavorites,
  formatFavoriteTime
}